package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.MobileLogin;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberZhyService;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/mobile")
public class MobileLoginController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberZhyService memberZhyService;

    @PostMapping("/login/smsLogin")
    public Result login(@RequestBody MobileLogin mobileLogin, HttpSession session) {
        log.info("手机登录数据{}{}", mobileLogin.getValidatecode(), mobileLogin.getTelephone());
        String telephone = mobileLogin.getTelephone();
        String code = mobileLogin.getValidatecode();

//        Object codeInSession = session.getAttribute(telephone);
        String codeInSession = (String) redisTemplate.opsForValue().get(telephone);
        if (codeInSession != null) {
            String[] split = codeInSession.split("\\.");
            if (split[1].equals(code) && split[0].equals("MOBILE_SIGNIN")) {
                LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Member::getPhoneNumber, telephone);
                Member member = memberZhyService.getOne(lqw);
                log.info("数据member{}", member);
//            判断是否是会员
                if (member == null) {
                    member = new Member();
                    member.setPhoneNumber(telephone);
                    member.setFileNumber(telephone);
                    Date date = new Date();
                    member.setRegTime(date);
                    memberZhyService.save(member);
                }
                session.setAttribute("member", member.getId());
                redisTemplate.delete(telephone);

                return new Result(true, MessageConst.LOGIN_SUCCESS, member);
            }
            return new Result(false, MessageConst.LOGIN_FAIL);
        }
        return new Result(false, MessageConst.LOGIN_FAIL);
    }

    /**
     * 获取验证码
     *
     * @param mobileLogin
     * @param session
     * @return
     */
    @PostMapping("validateCode/send")
    public Result sendMsg(MobileLogin mobileLogin, HttpSession session) {
        log.info("手机登录数据{}", mobileLogin.getTelephone());
        String telephone = mobileLogin.getTelephone();
        if (!StringUtils.isEmpty(telephone)) {
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}", code);
//            SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",telephone,code);
//            session.setAttribute(telephone,code);
            String typeCode = mobileLogin.getType() + "." + code;
            // 需要将生成的验证码保存到Redis,设置过期时间
            redisTemplate.opsForValue().set(telephone, typeCode, 5, TimeUnit.MINUTES);

            return new Result(true, MessageConst.ACTION_SUCCESS);
//            SMSUtils.sendMessage("阿里云短信测试","",telephone,code);

        }
        return new Result(false, MessageConst.ACTION_FAIL);

    }
}
